<?php

/**
 * Command class file, global options added:
 * --verbose
 * --help
 */
class ConsoleCommand extends CConsoleCommand
{
	public $verbose = false;
	public $help = false;

	public function init()
	{
		set_time_limit(0);
		ini_set('memory_limit', '1024M');
		// instant logger
		Yii::getLogger()->autoFlush = 1;
		Yii::getLogger()->autoDump = true;
		// console log
		foreach (Yii::app()->log->routes as $route) {
			if (!$route instanceof ConsoleLogRoute) {
				$route->enabled = false;
			}
		}
		parent::init();
	}

	protected function beforeAction($action, $params)
	{
		if ($this->help === true) {
			echo $this->getHelp();
			return false;
		}
		// verbose trace
		if ($this->verbose === true) {
			foreach (Yii::app()->log->routes as $route) {
				if ($route instanceof ConsoleLogRoute && $route->enabled) {
					$route->levels = 'trace, ' . $route->levels;
					break;
				}
			}
		}
		return parent::beforeAction($action, $params);
	}
}
